package com.corpcredit.webapp.action;

import java.io.Serializable;
import java.util.Date;

import com.corpcredit.Constants;
import com.corpcredit.model.Jbqk;
import com.corpcredit.model.Zygd;
import com.corpcredit.webapp.action.BasePage;
import com.corpcredit.service.GenericManager;

public class ZygdForm extends BasePage implements Serializable {
    private GenericManager<Zygd, Long> zygdManager;
    private Zygd zygd = new Zygd();
    private Long id;
    private Jbqk jbqk;
    public void setJbqk(Jbqk jbqk)
	{
		this.jbqk=jbqk;
	}
    public void setZygdManager(GenericManager<Zygd, Long> zygdManager) {
        this.zygdManager = zygdManager;
    }

    public Zygd getZygd() {
        return zygd;
    }

    public void setZygd(Zygd zygd) {
        this.zygd = zygd;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String delete() {
        zygdManager.remove(zygd.getId());
        addMessage("zygd.deleted");

        return "list";
    }

    public String edit() {
        // Comparison to zero (vs. null) is required with MyFaces 1.2.2, not with previous versions
        if (id != null && id != 0) {
            zygd = zygdManager.get(id);
        } else {
            zygd = new Zygd();
        }

        return "edit";
    }

    public String save() {
        boolean isNew = (zygd.getId() == null || zygd.getId() == 0);
        
        zygd.setJbqk(jbqk);
        zygd.setUsername(getRequest().getRemoteUser());
        zygd.setOptdate(new Date());
        zygd.setOperation(isNew?Constants.OPERATION_ADD:Constants.OPERATION_UPDATE);
        
        zygdManager.save(zygd);

        String key = (isNew) ? "zygd.added" : "zygd.updated";
        addMessage(key);

        if (isNew) {
            return "list";
        } else {
          	
     	   this.id=this.zygd.getId();
              return edit();
        }
    }
} 